import pymysql

db = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', database='test_123', charset='utf8')

# 创建游标
cur = db.cursor()
while True:
    name = input('Name:')
    age = int(input('Age:'))
    birthday = input('birthday yyyy-mm-dd:')

    # sql = 'insert into table1(name,age,birthday) values ("%s","%d","%s")' % (name, age, birthday)
    sql = 'insert into table1(name,age,birthday) values (%s,%s,%s)'
    try:
        # cur.execute(sql)
        cur.execute(sql, [name, age, birthday])
        # 增删改都需要提交
        db.commit()
    except Exception as e:
        # 失败回滚到操作之前
        db.rollback()
        print('Faild:', e)

cur.close()
db.close()
